﻿@model AllSettingsListModel

@using Giant.Services.Sites

@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Giant.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Giant.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.Configuration.Settings.AllSettings").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("All settings");
}

@Html.AntiForgeryToken()
<div class="content-header clearfix">
    <h1 class="pull-left">
        @T("Admin.Configuration.Settings.AllSettings")
    </h1>
    <div class="pull-right">
        &nbsp;
    </div>
</div>

<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.ExtLabelFor(model => model.SearchSettingName)
                                </div>
                                <div class="col-md-8">
                                    @Html.ExtEditorFor(model => model.SearchSettingName)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.ExtLabelFor(model => model.SearchSettingValue)
                                </div>
                                <div class="col-md-8">
                                    @Html.ExtEditorFor(model => model.SearchSettingValue)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-8 col-md-offset-4">
                                    <button type="button" id="search-settings" class="btn btn-primary btn-search">
                                        <i class="fa fa-search"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-body">
                    <div id="settings-grid"></div>

                    <script>
                        $(document).ready(function() {
                            $("#settings-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("AllSettings", "Setting"))",
                                            type: "POST",
                                            dataType: "json",
                                            contentType: "application/json",
                                            data: additionalData
                                        },
                                        create: {
                                            url: "@Html.Raw(Url.Action("SettingAdd", "Setting"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        update: {
                                            url: "@Html.Raw(Url.Action("SettingUpdate", "Setting"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("SettingDelete", "Setting"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        parameterMap: function(data, operation) {
                                            if (operation != "read") {
                                                return data;
                                            } else {
                                                //for some reasons only such "Filter" data be parsed
                                                return JSON.stringify(data);
                                            }
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "Id",
                                            fields: {
                                                Name: { editable: true, type: "string" },
                                                Value: { editable: true, type: "string" },
                                                Site: { editable: true, type: "string" },
                                                SiteId: { editable: true, type: "number" },
                                                Id: { editable: false, type: "number" }
                                            }
                                        }
                                    },
                                    requestEnd: function(e) {
                                        if (e.type == "create" || e.type == "update") {
                                            this.read();
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)]
                                },
                                toolbar: [{ name: "create", text: "@T("Admin.Common.AddNewRecord")" }],
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [
                                    {
                                        field: "Name",
                                        title: "@T("Admin.Configuration.Settings.AllSettings.Fields.Name")",
                                        width: 300
                                    }, {
                                        field: "Value",
                                        title: "@T("Admin.Configuration.Settings.AllSettings.Fields.Value")",
                                        width: 300
                                    }, {
                                        field: "SiteId",
                                        title: "@T("Admin.Configuration.Settings.AllSettings.Fields.SiteName")",
                                        width: 150,
                                        editor: siteDropDownEditor,
                                        template: "#:Site#"
                                    }, {
                                        command: [
                                            {
                                                name: "edit",
                                                text: {
                                                    edit: "@T("Admin.Common.Edit")",
                                                    update: "@T("Admin.Common.Update")",
                                                    cancel: "@T("Admin.Common.Cancel")"
                                                }
                                            }, {
                                                name: "destroy",
                                                text: "@T("Admin.Common.Delete")"
                                            }
                                        ],
                                        width: 200
                                    }
                                ]
                            });
                        });

                        //local datasource
                        var allSites = [
                            {
                                Id: 0,
                                Name: "@T("Admin.Configuration.Settings.AllSettings.Fields.SiteName.AllSites")"
                            }
                            @{
                                var siteService = EngineContext.Current.Resolve<ISiteService>();
                                var allSites = siteService.GetAllSites();
                            }
                            @foreach (var site in allSites)
                            {
                                <text>
                                , {
                                    Id: @(site.Id),
                                    Name: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(site.Name)))"
                                }
                                </text>
                            }
                        ];

                        function siteDropDownEditor(container, options) {
                            $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:SiteId"/>')
                                .appendTo(container)
                                .kendoDropDownList({
                                    autoBind: false,
                                    dataSource: allSites
                                });
                        }
                    </script>

                    <script type="text/javascript">
                        $(document).ready(function() {
                            $('#search-settings').click(function() {
                                var grid = $('#settings-grid').data('kendoGrid');
                                grid.dataSource.page(1);
                                return false;
                            });
                        });

                        function additionalData() {
                            var data = {
                                SearchSettingName: $('#@Html.FieldIdFor(model => model.SearchSettingName)').val(),
                                SearchSettingValue: $('#@Html.FieldIdFor(model => model.SearchSettingValue)').val()
                            };
                            addAntiForgeryToken(data);
                            return data;
                        }

                        $("".concat("#@Html.FieldIdFor(model => model.SearchSettingName),",
                            "#@Html.FieldIdFor(model => model.SearchSettingValue)")).keydown(function(event) {
                                if (event.keyCode === 13) {
                                    $("#search-settings").click();
                                    return false;
                                }
                            });
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>